<?php
namespace App\SDK\PingAn\library;

use App\SDK\PingAn\src\MySm2;

 class JWT
{
    private $_certs;
    private $config = '';
    private $sm2 = '';

    public function __construct(){
        $this->config = Config::get();
        $this->sm2 = new MySm2('hex');

    }

    public function sign($str)
    {
        $header['kid']=$this->config['app_id'];
        $header['typ']='JWT';
        $header['alg']='SM2';
        $header = substr(base64_encode(json_encode($header)),0,-1);
        $b = base64_encode(json_encode([]));
        $document=$header.'.'.$str;
        $sign = $this->sm2->doSign( $document,$this->config['private_key']);

        $s = $header.'.'.'e30'.'.'.base64_encode($sign);

        return $s;
    }

    //云收款form表单需要排序加签
    public function sortSign($str)
    {
            $header['kid']=$this->config['app_id'];
            $header['typ']='JWT';
            $header['alg']='SM2';
            $header = substr(base64_encode(json_encode($header)),0,-1);
//            var_dump($header);
//            exit();
            $b = base64_encode(json_encode([]));
            $document=$header.'.'.$this->sortStr($str);
            $sign = $this->sm2->doSign( $document,$this->config['private_key']);

            $s = $header.'.'.'e30'.'.'.base64_encode($sign);

            return $s;
    }

    private function sortStr($str)
    {
        if (is_array($str)) {
            $arr = []; $result = [];
            $this->assemble('', $str, $arr);
            ksort($arr);
            foreach ($arr as $key => $value) {
                $result[] = $key . '=' . $value;
            }
            $str = implode('&', $result);
        }
        return $str;
    }

    public function isValid($str, $signature)
    {
        $publicKey = $this->config['public_key'];
        if ($publicKey)
        {
            $arr = explode('.',$signature);
            if(count($arr)==3){
                $document=$arr[0].'.'.$str;
                $sign = base64_decode($arr[2]);
                $result = $this->sm2->verifySign($document,$sign,$publicKey,'');
            }else{
                $result = false;
            }

        } else {
            //public key resource identifier False!
            $result = false;
        }
        return $result;
    }

    //云收款form表单需要排序验签
    public function isSortValid($str, $signature)
    {
        $publicKey = $this->config['public_key'];
        if ($publicKey)
        {
            $data = $this->sortStr($str);
            $arr = expload('.',$signature);
            if(count($arr)==3){
                $document=$arr[0].'.'.json_encode($data);
                $sign = base64_decode($arr[2]);
                $result = $this->sm2->verifySign($document,base64_decode($sign),$publicKey);
            }else{
                $result = false;
            }

        } else {
            //public key resource identifier False!
            $result = false;
        }
        return $result;
    }

    private function assemble($prev, $data, &$result) {
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $this->assemble($prev . (is_string($key) ? $key : ''), $value, $result);
            } else {
                /*if ($value !== '') {
                    $result[$prev . $key] = $value;
                }*/
                $result[$prev . $key] = $value;
            }
        }
        return $result;
    }
}
